package com.future.mayi.qsale.utils;

import android.app.Activity;
import android.support.annotation.NonNull;

import com.imnjh.imagepicker.SImagePicker;
import com.future.mayi.utils.perimission.DefaultRationale;
import com.future.mayi.utils.perimission.PermissionSetting;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.List;

/**
 * Created by Administrator on 2018/8/15.
 */

public class SelectImgUtils {

    public static final int REQUEST_CODE_IMAGE = 100;
    private DefaultRationale mRationale;
    private PermissionSetting mSetting;


    public void selectImg(final Activity activity, final int maxSelectBum) {
        if (mRationale == null) {
            mRationale = new DefaultRationale();
        }
        if (mSetting == null) {
            mSetting = new PermissionSetting(activity);
        }
        AndPermission.with(activity)
                .permission(Permission.CAMERA, Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE)
                .rationale(mRationale)
                .onGranted(new Action() {
                    @Override
                    public void onAction(List<String> permissions) {
                        SImagePicker
                                .from(activity)
                                .maxCount(maxSelectBum)
                                .rowCount(3)
                                .showCamera(true)
                                .pickMode(SImagePicker.MODE_IMAGE)
                                .forResult(REQUEST_CODE_IMAGE);
                    }
                })
                .onDenied(new Action() {
                    @Override
                    public void onAction(@NonNull List<String> permissions) {
                        if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
                            mSetting.showSetting(permissions);
                        }
                    }
                })
                .start();
    }


}
